/*****************************************************************************
 * Copyright (C) Stacy Curl. All rights reserved.                            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *****************************************************************************/
package picounit.features.failures_first;

import java.io.IOException;
import java.io.Writer;
import java.util.Map;

class ProblemCount implements Comparable<ProblemCount>  {
	private final String testClass;

	private int errorCount;
	private int failureCount;

	public static ProblemCount getOrCreate(Map<String, ProblemCount> map, Class testClass) {
		if (map.containsKey(testClass.getName())) {
			return map.get(testClass.getName());
		}
		else {
			return new ProblemCount(testClass).addTo(map);
		}
	}

	public ProblemCount(String currentLine) {
		this.testClass = currentLine.substring(0, currentLine.indexOf('='));
		
		this.errorCount = Integer.parseInt(currentLine.substring(currentLine.indexOf('=') + 1, currentLine.indexOf(',')));

		this.failureCount = Integer.parseInt(currentLine.substring(currentLine.indexOf(',') + 1));
	}

	public ProblemCount(Class testClass) {
		this(testClass, 0, 0);
	}

	public ProblemCount(Class testClass, int errorCount, int failureCount) {
		this.testClass = testClass.getName();
		this.failureCount = failureCount;
		this.errorCount = errorCount;
	}
	
	public void increaseErrors() {
		errorCount++;
	}
	
	public void increaseFailures() {
		failureCount++;
	}

	public ProblemCount addTo(Map<String, ProblemCount> problemMap) {
		problemMap.put(testClass, this);
		
		return this;
	}

	public void writeExternal(Writer writer) throws IOException {
		if (errorCount > 0 || failureCount > 0) {
			writer.write(testClass + '=' + errorCount + ',' + failureCount + "\r\n");
		}
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Comparable<ProblemCount> Methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	public int compareTo(ProblemCount other) {
		if (errorCount > other.errorCount) {
			return -1;
		}
		else if (errorCount < other.errorCount) {
			return 1;
		}
		else if (failureCount > other.failureCount) {
			return -1;
		}
		else if (failureCount < other.failureCount) {
			return 1;
		}
		else {
			return 0;
		}
	}

	///////////////////////////////////////////////////////////////////////////////////////////////
	// Object Methods
	///////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public String toString() {
		return testClass + " " + failureCount + " failures, " + errorCount + " errors\n";
	}
}